/*----------------------------------------------------
*  This file is for post comment
-----------------------------------------------------*/
var OK = 'ok', _div = 'comment';
var A = 'post-comment', _area = A + '-area', _form = A + '-form', _btn = A + '-submit';
var _btn_val = '', _btn_msg = 'Sending...', _tip_msg = 'Loading...';
$(function(){
	/*---- other ------------------------------------*/
	$('.new-common .add').click(function(){
		$(this).hide();
		$('#' + _area).show();
	});
	$.postComment.showEditor();
	/*---- binding reply ---------------------------*/
	$('ol.commentlist li').each(function(i){
		var obj = $(this);
		obj.find('.op a.reply').click(function(){
			$.postComment.reply(obj);
		});
		obj.find('.op a.quote').click(function(){
			$.postComment.quote(obj);
		});
		var obj2 = obj.find('.op a.good, .op a.bad');
		if ($.cookie("good-bad-" + obj.attr('id'))) {
			obj2.addClass('click');
		} else {
			obj2.one("click", function(){
				$.postComment.good_bad($(this), obj, obj2);
			});
		}
	});
	/*---- from ------------------------------------*/
	$('#' + _form).ajaxForm({ 
		type    : 'post',
        dataType: 'json', 
		beforeSubmit: $.postComment.form.before,
        success: $.postComment.form.success
    }); 
	$('#captcha').one('click', function(){
		$('.captcha_tips').html('&laquo; Click this to refresh a new captcha');
		$('.captcha_right').fadeIn("slow");
	});
	
	/*---- tips ------------------------------------*/
	$.postComment.tips.init('#' + _btn);
	$('.op a.report').poshytip({
		showOn: 'hover',
		className: 'tip-yellowsimple',
		alignTo: 'target',
		alignX: 'center',
		alignY: 'top',
		content: 'Sorry, will support later'
	});
});

$.postComment = {
	showEditor: function(){
		CKEDITOR.replace(_div, {
			toolbar: 'MyBasic',
			width: 600,
			height: 300,
			resize_enabled: true,
			resize_dir: 'vertical'
		});
		$('.adjustEditor').show();
	},
	reply: function(o){
		var html = "<div class='ref'><h5>Reference \"" + o.find('h4').text() + "\" Comment</h5>";
		html += "<div class='ref_body'>"+o.find('.msg').html()+"</div></div><p>";
		$.postComment.setData(html);
	},
	quote: function(o){
		$.postComment.setData("<div id='q'>"+o.find('.msg').html()+"</div><p>");
	},
	good_bad: function(o, o1, o2){
		o2.unbind('click');
		o2.addClass('click');
		b = o.find("b");
		b.text(parseInt(b.text()) + 1);
		$.cookie("good-bad-" + o1.attr('id'), '1', { expires: 1 });
		$.common.ajax('/comment/digg', {id : $('#postId').val(), type : o.hasClass('good')},
			function(data){
				alert(data);
			}
		);
	}, 
	setData: function(obj) {
		$('.new-common .add').hide();
		$('#' + _area).show();
		CKEDITOR.instances.comment.insertHtml(obj);
	},
	tips : {
		init: function(obj) {
			$(obj).poshytip({
				showOn: 'none',
				timeOnScreen : 3000,
				className: 'tip-yellowsimple',
				alignTo: 'target',
				alignX: 'center',
				alignY: 'top',
				offsetX: 5,
				content: $.postComment.tips.updateCb
			});
		},
		updateCb : function () {
			return _tip_msg;
		},
		show : function(obj, msg) {
			_tip_msg = msg;
			$(obj).poshytip('show');
		}
	},
	form : {
		validate: function() {
			var btn = $('#' + _btn);
			var id = 'commentator';
			if (!$.common.test(/^\w{2,15}$/, $('#'+id).val())) {
				$.postComment.tips.show(btn, $.cde.msg['BL_CO_001']);
				return false;
			}
			id = 'email';
			if (!$.common.email($('#'+id).val())) {
				$.postComment.tips.show(btn, $.cde.msg['CO_001']);
				return false;
			}
			id = 'link';
			if (!$.common.blank($('#'+id).val()) && !$.common.url($('#'+id).val())) {
				$.postComment.tips.show(btn, $.cde.msg['CO_002']);
				return false;
			}
			id = 'comment';
			var oEditor = CKEDITOR.instances.comment;
			if ($.common.blank(oEditor.getData()) || oEditor.getData().length < 5) {
				$.postComment.tips.show(btn, $.cde.msg['BL_CO_002']);
				return false;
			}
			id = 'captcha';
			if (!$.common.test(/^\w{4}$/, $('#'+id).val())) {
				$.postComment.tips.show(btn, $.cde.msg['CP_001']);
				return false;
			}
			$.postComment.form.start();
			return true;
		},
		before: function() {
			return $.postComment.form.validate();
		},
		start: function() {
			_btn_val = $('#' + _btn).val();
			$.common.btnStatus(_btn, true, _btn_msg);
		},
		complete: function() {
			$.common.btnStatus(_btn, false, _btn_val);
		},
		success: function(data) {
			$.postComment.form.complete();
			if (OK === data) {
				window.location.reload();
			} else {
				$.postComment.tips.show($('#' + _btn), $.cde.msg[data]);
			}
		}
	},
};
